<?php
require_once 'config21.php';
if (!isset($_SESSION['username'])) {
    header('Location: login21.php');
    exit();
}

if (isset($_GET['id'])) {
    $teaching_id = $_GET['id'];
    $sql = "SELECT teaching.*, courses.course_name, teachers.teacher_name 
            FROM teaching 
            JOIN courses ON teaching.course_id = courses.course_id 
            JOIN teachers ON teaching.teacher_id = teachers.teacher_id 
            WHERE teaching_id = $teaching_id";
    $result = $conn->query($sql);
    if ($result->num_rows == 1) {
        $row = $result->fetch_assoc();
    } else {
        echo "未找到该授课信息";
        exit;
    }
} else {
    echo "缺少授课 ID";
    exit;
}

// 获取所有课程列表
$courses_sql = "SELECT course_id, course_name FROM courses";
$courses_result = $conn->query($courses_sql);

// 获取所有教师列表
$teachers_sql = "SELECT teacher_id, teacher_name FROM teachers";
$teachers_result = $conn->query($teachers_sql);

// 修改授课信息
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST['edit_teaching'])) {
    $course_id = $_POST['course_id'];
    $teacher_id = $_POST['teacher_id'];
    $semester = $_POST['semester'];

    $sql = "UPDATE teaching 
            SET course_id = '$course_id', 
                teacher_id = '$teacher_id', 
                semester = '$semester' 
            WHERE teaching_id = $teaching_id";
    if ($conn->query($sql)) {
        header('Location: teaching_management21.php');
        exit;
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
?>

<!DOCTYPE html>
<html lang="zh-hans">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改授课信息</title>
    <link rel="stylesheet" href="style.css">
    <style>
        form {
            max-width: 500px;
            margin: 20px auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        select, input[type="text"] {
            width: 100%;
            padding: 8px;
            margin-bottom: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        input[type="submit"] {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #0056b3;
        }
        .back-link {
            display: block;
            text-align: center;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <h2>修改授课信息</h2>
    <form action="<?php echo $_SERVER['PHP_SELF'] . "?id=$teaching_id" ?>" method="post">
        <label for="course_id">课程:</label>
        <select name="course_id" required>
            <?php
            while ($course = $courses_result->fetch_assoc()) {
                $selected = ($course['course_id'] == $row['course_id']) ? 'selected' : '';
                echo "<option value='" . $course['course_id'] . "' $selected>" . 
                     $course['course_name'] . "</option>";
            }
            ?>
        </select>

        <label for="teacher_id">教师:</label>
        <select name="teacher_id" required>
            <?php
            while ($teacher = $teachers_result->fetch_assoc()) {
                $selected = ($teacher['teacher_id'] == $row['teacher_id']) ? 'selected' : '';
                echo "<option value='" . $teacher['teacher_id'] . "' $selected>" . 
                     $teacher['teacher_name'] . "</option>";
            }
            ?>
        </select>

        <label for="semester">学期:</label>
        <input type="text" name="semester" value="<?php echo $row['semester']; ?>" 
               placeholder="例如：2023-2024-1" required>

        <input type="submit" name="edit_teaching" value="修改">
    </form>
    <a href="teaching_management21.php" class="back-link">返回授课管理页面</a>
</body>
</html>